package com.mrx.xcitools.dao;

import com.mrx.xcitools.model.RepositoryConfig;
import jakarta.annotation.PostConstruct;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author Mr.X
 * @since 2024-03-10 下午 5:35:10
 */
@Repository
public class RepositoryConfigMapper {

    private static final List<RepositoryConfig> configs = new CopyOnWriteArrayList<>();

    @PostConstruct
    public void init() {
        configs.add(RepositoryConfig.of(
                1, "x-browser-sync-server",
                "git@gitee.com:zhReimu/x-browser-sync-server.git",
                "master", "x-browser-sync-server", null
        ));
    }

    public void insert(RepositoryConfig config) {
        configs.add(config);
    }

    public List<RepositoryConfig> listAll() {
        return Collections.unmodifiableList(configs);
    }

    public RepositoryConfig selectById(Integer id) {
        return listAll().stream().filter(it -> it.getId().equals(id)).findFirst().orElse(null);
    }
}
